package com.qust.enDecrpytion.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Verification {
    @SafeVarargs
    public static boolean methodAnnotationDuplicateVerification(Method method, Class<? extends Annotation>... annotations) {
        if (annotations.length < 2) {
            throw new RuntimeException("注解类型小于1个");
        }
        int verification = 0;
        for (Class<? extends Annotation> annotation : annotations) {
            if (method.isAnnotationPresent(annotation)) {
                verification += 1;
            }
        }
        if (verification >= 2) {
            throw new RuntimeException(method.getName() + "注解重复修饰");
        }
        return true;
    }

    @SafeVarargs
    public static boolean parametersAnnotationDuplicateVerification(Method method, Class<? extends Annotation>... annotations) {
        Parameter[] parameters = method.getParameters();
        int[] verificationArr = new int[parameters.length];
        for (Parameter parameter : parameters) {
            int verification = 0;
            for (Class<? extends Annotation> annotation : annotations) {
                if (parameter.isAnnotationPresent(annotation)) {
                    verification += 1;
                }
            }
            if (verification >= 2) {
                throw new RuntimeException(method.getName() + " : " + parameter.getName() + ",注解重复修饰");
            }
        }
        return true;
    }

    @SafeVarargs
    public static boolean isUseAnnotations(Method method, Class<? extends Annotation>... annotations) {
        Parameter[] parameters = method.getParameters();
        for (Class<? extends Annotation> annotation : annotations) {
            if (method.isAnnotationPresent(annotation)) {
                return true;
            }
            for (Parameter parameter : parameters) {
                if (parameter.isAnnotationPresent(annotation)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断方法返回类型是否为target类型
     *
     * @param method 方法
     * @param target 类型
     * @return 相同true, 不相同false
     */
    public static boolean returnTypeIsTarget(Method method, Class<?> target) {
        return method.getReturnType() == target;
    }

}
